home *** CD-ROM | disk | FTP | other *** search
/ The Business Master (3rd Edition) / The Business Master (3rd Edition).iso / files / texttors / nyedit21 / macros.arc / QHELP < prev    next >
Encoding:
Text File  |  1988-06-18  |  3.2 KB  |  146 lines

  1. /*
  2. ** QuickHelp macro
  3. ** Gives help on the word underneath the cursor
  4. ** First macro written by William B. McCormick
  5. */
  6.  
  7.  
  8. #define ALT_K   165
  9. #define FNAME   "qhelp.$$$"
  10.  
  11.  
  12. init()
  13. {
  14.   assign_key("qhelp", ALT_K);
  15. }
  16.  
  17. /*
  18. ** WordChar:
  19. **  return TRUE if the character specified is an OK part of a word
  20. */
  21. WordChar(str)
  22.   string str;
  23. {
  24.   return ((str >= "a" && str <= "z") ||
  25.           (str >= "A" && str <= "Z") ||
  26.           (str >= "0" && str <= "9") ||
  27.           (str == "_"));
  28. }
  29.  
  30. /*
  31. ** Call QuickHelp to get help on the word underneath the cursor
  32. */
  33. qhelp()
  34. {
  35.   string str;
  36.   int first, last, len;
  37.   int oldbuf, newbuf;
  38.  
  39.   /*
  40.    * * save our position so we can go right back to where we * were before we
  41.    * hit ALT_K * Don't forget to restore_position() before any return's 
  42.    */
  43.   save_position();
  44.  
  45.   /* get the current line */
  46.   str = currline();
  47.  
  48.   /*
  49.    * * get the line length so we don't have to keep * computing it.  This may
  50.    * be no big deal. 
  51.    */
  52.   len = strlen(str);
  53.  
  54.   /*
  55.    * * move to beginning of current word by moving backwards until * we find
  56.    * a character that doesn't belong in the word. * if the character we start
  57.    * on doesn't belong, it's OK -- we'll * catch it after we find the last
  58.    * character 
  59.    */
  60.   for (first = currcol(); first > 1; --first)
  61.   {
  62.     if (!WordChar(substr(str, first, 1)))
  63.     {
  64.       ++first;
  65.       break;
  66.     }
  67.   }
  68.  
  69.   /*
  70.    * * move to end of current word * same method as before 
  71.    */
  72.   for (last = currcol(); last <= len; ++last)
  73.   {
  74.     if (!WordChar(substr(str, last, 1)))
  75.     {
  76.       --last;
  77.       break;
  78.     }
  79.   }
  80.  
  81.   /*
  82.    * * here's where we catch the advance/retreat problems.  If the *
  83.    * character we started on doesn't belong in a word, first will * point to
  84.    * the character after -- last will point to the * character before. 
  85.    * (Except in col 1 - I should fix that...) 
  86.    */
  87.   if (last < first)
  88.   {
  89.     /* no word, so complain */
  90.     message("No word under cursor");
  91.     get_tty_char();
  92.     restore_position();
  93.     return;
  94.   }
  95.  
  96.   /* get the word we're searching for */
  97.   str = substr(str, first, last - first + 1);
  98.  
  99.   /* delete the file -- so we can tell if QH messes up */
  100.   delete_file(FNAME);
  101.  
  102.   /* call QuickHelp to get the text */
  103.   if (os_command(sprintf("qh %s -p %s -t all", str, FNAME)) < 0)
  104.   {
  105.     message("Can't call QuickHelp");
  106.     get_tty_char();
  107.     restore_position();
  108.     return;
  109.   }
  110.  
  111.   /* now display the help in a window */
  112.  
  113.   /* save the old buffer id (so we can go back) */
  114.   oldbuf = currbuf();
  115.  
  116.   /* create a new buffer with the help text */
  117.   newbuf = create_buffer(FNAME);
  118.  
  119.   /* and delete the file to keep the disk clean */
  120.   delete_file(FNAME);
  121.  
  122.   setcurrbuf(newbuf);
  123.  
  124.   /*
  125.    * * if no text in the window, QH messed up.  Don't worry about an * error
  126.    * message, QH took care of it 
  127.    */
  128.   if (lastlinenum() <= 1)
  129.   {
  130.     delete_buffer(newbuf);
  131.     setcurrbuf(oldbuf);
  132.     restore_position();
  133.     return;
  134.   }
  135.  
  136.   /* display the help text */
  137.   show_buffer(newbuf);
  138.  
  139.   /* display the old text */
  140.   show_buffer(oldbuf);
  141.  
  142.   /* and go back to the same position */
  143.   restore_position();
  144.   return;
  145. }
  146.